package com.fit.pages.homeworkRecords;

import java.util.Date;
import java.util.List;

import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.internal.antlr.PropertyExpressionParser.list_return;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.services.PropertyAccess;
import org.apache.tools.ant.taskdefs.Sleep;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.fit.annotations.AdminAccess;
import com.fit.annotations.AnonymousAccess;
import com.fit.annotations.ProfessorAccess;
import com.fit.annotations.StudentAccess;
import com.fit.dao.studentOfTheCourse.StudentOfTheCourseDAO;
import com.fit.dao.course.CourseDAO;
import com.fit.dao.homework.HomeworkDAO;
import com.fit.dao.homeworkRecords.HomeworkRecordsDAO;
import com.fit.dao.homeworkRecords.HomeworkRecordsDAOImpl;
import com.fit.dao.language.LanguageDAO;
import com.fit.dao.user.UserDAO;
import com.fit.entities.studentOfTheCourse.StudentOfTheCourse;
import com.fit.entities.course.Course;
import com.fit.entities.homework.Homework;
import com.fit.entities.homeworkRecords.HomeworkRecords;
import com.fit.entities.language.Language;
import com.fit.entities.user.User;
import com.fit.lov.generic.GenericSelectModel;
import com.fit.lov.user.UserSelectModel;
import com.fit.pages.ErrorPage;
import com.fit.services.Authenticator;

@AdminAccess
@AnonymousAccess
@ProfessorAccess
@StudentAccess
public class Add {

	private GenericSelectModel<User> userModel;
	private GenericSelectModel<Homework> homeworkModel;

	// Inject
	@Inject
	private PropertyAccess access;
	@Inject
	private Session session;
	@Inject
	private Messages messages;
	@Inject
	private Authenticator authenticator;
	@Inject
	private UserDAO userDAO;
	@Inject
	private HomeworkDAO homeworkDAO;
	@Property
	private User user;
	@Property
	private HomeworkRecords homeworkRecords;



	@Persist
	@Property
	private User selectedUser;
	@Persist
	@Property
	private int points;
	@Property
	@Persist
	private Homework selectedHomework;
	
	public GenericSelectModel<User> getUserModel(){
		List<User> s = session.createCriteria(User.class).add(Restrictions.sqlRestriction("role = 2")).list();
		userModel = new GenericSelectModel<User>(s, User.class, "userName", "id", access);
		return userModel;
	}
	
	
	
	public GenericSelectModel<Homework> getHomeworkModel() {
		List<Homework> c = homeworkDAO.findAll();
		homeworkModel = new GenericSelectModel<Homework>(c, Homework.class, "task", "homework", access);
		return homeworkModel;
	}

	
	

	Object onSubmitFromAddTestForm()
	{
		System.out.println("The form was submitted!");
		return null;
	}


	@OnEvent(value="selected", component="submitButton")
	void onSubmitButton()
	{
		HomeworkRecords c = new HomeworkRecords();

		c.setHomework(selectedHomework);
		System.out.println(selectedHomework+"-------------------------------aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
		c.setUser(selectedUser);
		c.setPoints(points);
		session.save(c);
		selectedHomework = null;
		selectedUser = null;
		points=0;
	}

	@OnEvent(component = "ResetConfLink")
	Object onResetConfLink() {
		return null;
	}

	@OnEvent(component = "ResetErrorLink")
	Object onResetErrorLink() {
		return null;
	}



}
